<?php
require "admin/model/Model.php";
class foodModel extends Model
{
	function __construct()
	{
		parent::__construct();
		$this->mod='foods';
	}
	function getTable()
	{
		$sql="select * from {$this->mod}";
		$data=$this->conn->query($sql);
		// var_dump($data);
		return $data;
	}
	function add($data)
	{

    $foodname=$data['foodname'];
    $tid=$data['tid'];
    $intro=$data['intro'];
  
		
   if($_FILES["pic"]["error"]>0)
    switch ($_FILES["pic"]["error"])
    {
      case 1:echo "文件尺寸超过了配置文件的最大值";exit;
      case 3:echo "部分文件上传";exit;
      default:echo "未知错误";exit;
    }
    else
    {
      //获取文件扩展名
      $suffix=strrchr($_FILES["pic"]["name"],'.');//获取在文件中最后一次出现
        //判断文件类型是否是图片
      $allowtype=array("jpg","jpeg","png","gif","bmp","flv","JPG","JPEG","PNG","GIF","BMP","FLV");
      if(!in_array(ltrim($suffix,'.'),$allowtype))
      {
        echo "文件类型为$suffix!<br/>";
        echo "文件类型不正确！只能选择扩展名为jpg,jpeg,png,gif,bmp,flv类型的文件！";
        exit;
      }
      //指定在服务器上的文件存放路径和文件名
      $filepath=dirname(__FILE__).'/'."../../upload/";
      $newname=date("YmdHis").rand(100,999).$suffix;
      $upload=date("YmdHis");
      
      if(!move_uploaded_file($_FILES["pic"]["tmp_name"],$filepath.$newname))
        die('图片上传失败');
      else
        $sql = "insert into {$this->mod} values(null,'$foodname','$tid','$newname','$intro','$upload',null,null,null,null)";
    }
			  $result = $this->conn->query($sql);
			  return $result;
	}
	function getById($id)
	{
		$sql="select * from {$this->mod} where vid=".$id;
		$result=$this->conn->query($sql);
		return $result;	
	}
	function update($data){
		$id=trim($_POST['id']);
		$foodname=trim($_POST['foodname']);
		$tid=$_POST['tid'];
		$address=$_POST['address'];
		$intro=$_POST['intro'];
			if($_FILES["pic"]["error"]>0)
			{
				switch($_FILES["pic"]["error"])
				{
					case 1: echo "文件尺寸超过了配置文件的最大值";exit;
   					case 3: echo "部分文件上传";   exit;
    				case 4: echo "没有选择头像文件！";
    				//如果没选择图片，则直接更新其他数据
    				$sql="update {$this->mod} set foodname='{$foodname}',vid={$vid},intro='{$intro}',address='{$address}' where vid={$id}";
    				break;
    				default: echo "未知错误"; exit;
  				}
  			}else{
  				$suffix=strrchr($_FILES["pic"]["name"],'.');//获取.在文件名中最后一次出现
		  		$allowtype=array("jpg","jpeg","png","gif","Bmp","flv");
  				if(!in_array(ltrim($suffix,'.'),$allowtype))
  					{
  						echo "文件类型为$suffix!<br/>";
  						echo "文件类型不正确！只能选择扩展名为jpg,jpeg,png,gif,Bmp,flv类型的文件！";
  						exit;
  					}
 
  				$filepath='upload/';
  				$newname=date("YmdHis").rand(100,999).$suffix;
  				if(!move_uploaded_file($_FILES["pic"]["tmp_name"],$filepath.$newname))
 					{
    					die('图片上传失败');
   					}
   				else{
    					$sql="select * from {$this->mod} where tid={$id}";
						$result=mysql_query($sql);
						$row=mysql_fetch_assoc($result);
						$filename='$filepath'.$row["pic"];
						if(file_exists($filename))    unlink($filename);
						$sql="update {$this->mod} set foodname='{$foodname}',tid='{$tid}',pic='{$newname}',intro='{$intro}',address='{$address}' where vid={$id}";
 					 }
 				$result = $this->conn->query($sql);
				return $result;
			}
	}
	function del($id){
			$sql = "delete from {$this->mod} where vid=".$id;
			$result = $this->conn->query($sql);
			return $result;
	}
}
?>